package ps.server;

import java.awt.AWTException;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.util.Properties;

import javax.swing.ImageIcon;




public class ServerApp {
    
    public static final String CONFIG_FILE = "ps-server.ini";

	boolean startWithTrayIcon = false;
    SystemTray tray = null;
    ImageIcon icon = new ImageIcon(getClass().getResource("icon.png"));
    TrayIcon trayIcon = null;
    Properties serverProps = new Properties();
    
    ServerPacketHandler server;
    
    private void registerTrayIcon() {

        if (SystemTray.isSupported()) {
    
            tray = SystemTray.getSystemTray();
    
            ActionListener shutdownListener = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    try {
                        shutdown();
                        System.exit(0);
                    } catch (Exception ex) {
                    	System.err.println(ex.getMessage());
                    	System.exit(-1);
                    }
                }
            };
                    
            PopupMenu popup = new PopupMenu();
            MenuItem shutdownItem = new MenuItem("Server herunterfahren");
            shutdownItem.addActionListener(shutdownListener);
            popup.add(shutdownItem);
    
            trayIcon = new TrayIcon(icon.getImage(), "ParseSharer-Server", popup);
    
//            ActionListener actionListener = new ActionListener() {
//                public void actionPerformed(ActionEvent e) {
//                    trayIcon.displayMessage("Action Event", 
//                        "An Action Event Has Been Performed!",
//                        TrayIcon.MessageType.INFO);
//                }
//            };
                    
            trayIcon.setImageAutoSize(true);
//            trayIcon.addActionListener(actionListener);
    
            try {
                tray.add(trayIcon);
            } catch (AWTException ex) {
                System.err.println("Tray icon could not be added.");
            }
    
        } else {
            System.out.println("Tray icon is not supported.");
        }
    }
    
    void displayInfoMessage(String title, String msg) {
        if (trayIcon != null) {
            trayIcon.displayMessage(title, msg, TrayIcon.MessageType.INFO);
        }
    }
    
    public void startWithTrayIcon() throws Exception {
        startWithTrayIcon = true;
        startServer();
    }
    
    public void startServer() throws Exception {
        if (server == null) {
            loadConfigFile();
            
            server = new ServerPacketHandler(serverProps);
            
            if (startWithTrayIcon) {
                registerTrayIcon();
            }
            
            System.out.println("Server wurde erfolgreich gestartet.");
            
            if (startWithTrayIcon) {
                displayInfoMessage("ParseSharer-Server", "Server wurde erfolgreich gestartet.");
            }
        }
    }
    
    public void shutdown() throws Exception {
        if (server == null) {
            throw new Exception("Server ist nicht gestartet.");
        } else {
        	System.out.println("Server wird jetzt beendet.");
            
            if (tray != null && trayIcon != null) {
                tray.remove(trayIcon);
            }
            
            server.shutdown();
            server = null;
            
            System.out.println("Server wurde erfolgreich beendet.");
        }
    }
    
    private void loadConfigFile() {
        try {
            serverProps.load(new FileReader(CONFIG_FILE));
            System.out.println("CONFIG FILE LOADED: " + CONFIG_FILE);
        } catch (Exception ex) {
            System.out.println("LOAD CONFIG FILE: " + ex.getMessage());
        }
    }
    
}
